<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>意达设计</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/member.css"/>

</head>
<body class="body">

<!-- header -->
<div class="pageheader"><a href="javascript:void(0);" onclick="api.closeWin();" class="return"></a><span>选择收货地址</span></div>

<!-- contact -->
<div class="hr hrBG3"></div>

<div class="adreul" id='con'>
	<!-- <div class="adreli">
        <div class="addre_n1"><span>张三</span><span>15716881688</span></div>
        <div class="addre_n2">重庆市南岸区南坪步行街惠工路19号贝迪龙庭A座14-2</div>
        <div class="addre_n3">
            <div class="adrespan"><label class="adreOn">默认地址<input type="radio" checked="checked" name="adres"></label></div>
            <div class="adrecaz">
                <a href="javascript:void(0)" class="bam1">编辑</a>
                <a href="javascript:void(0)" class="bam2">删除</a>
            </div>
        </div>
    </div> -->
	
</div>

<div class="sumeNull"></div>
<input type="button" value="添加新地址" class="btmadres" id='add' onclick="add()"></div>


</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../script/adaptive.js"></script>
<script type="text/javascript" src="../script/tabs.js"></script>
<script type="text/javascript">
    window['adaptive'].desinWidth = 750;
    window['adaptive'].baseFont = 28;
    window['adaptive'].maxWidth = 750;
    window['adaptive'].init();

    


    var page=1;
    var p='';
    apiready = function () {
        $api.fixStatusBar($api.dom('.header'));
        api.addEventListener({
            name: 'keyback'
        }, function(ret, err){
            api.closeWidget();
        });
        //api.showProgress();

        
        var para=api.pageParam;
        

    	

        toDoRequest();

        
        api. addEventListener({name:'scrolltobottom'}, function(ret, err){
            
            toDoRequest();

        });
        
      
    


    };
    function chooseaddr(dd){
        var para=api.pageParam

        if(para.gid>0 || para.type=='cartaddr'){

            var txt=$('#a_'+dd).html();
            $api.setStorage('addrid',dd);
            $api.setStorage('addrtxt',txt);
            //addrid=id;
            
            if(para.gid>0){

                api.openWin({
                    name: 'loginName',
                    url: './goodsdetail.html',
                    reload:true,
                    pageParam: {
                        id:para.gid,
                        aid:dd,
                        
                    }
                });
            }else if(para.type=='cartaddr'){
                api.openWin({
                    name: 'loginName',
                    url: './cart.html',
                    reload:true,
                    pageParam: {
                        aid:dd,
                        
                        
                    }
                });
            }
        }

       
        
    }

    function toDoRequest(){

        p={token:token,page:page};

        api.ajax({
            url: webSiteUrl+'/app/user/myaddr',
            method: 'post',
            data: {
                values:p,
            }
        }, function(ret, err) {
            api.hideProgress();
            
            if(ret.code==200){
                
                //alert(ret.msg);
              
                html = "";
                var tj=ret.data;
               
                
for(var i=0;i<tj.length;i++){

html += '<div class="adreli" id="p'+tj[i].id+'"><div class="addre_n1"><span>'+tj[i].name+'</span><span>'+tj[i].phone+'</span></div><div class="addre_n2" onclick="chooseaddr('+tj[i].id+')" id="a_'+tj[i].id+'">'+tj[i].city+tj[i].addr+'</div><div class="addre_n3">';

        if(tj[i].default==1){
html +='<div class="adrespan"><label class="adreOn" id="'+tj[i].id+'" onclick="setdef('+tj[i].id+')">默认地址<input type="radio" checked="checked" name="adres"></label></div>';
        }else{
html += '<div class="adrespan"><label id="'+tj[i].id+'" onclick="setdef('+tj[i].id+')">默认地址<input type="radio" name="adres"></label></div>';        
        }
            
html += '<div class="adrecaz"><a href="javascript:void(0)" class="bam1" onclick="edit('+tj[i].id+')">编辑</a><a href="#" class="bam2" onclick="del('+tj[i].id+')">删除</a></div></div></div>';

}

                if(page==1){
                    $('#con').html(html);
                }else{
                    $('#con').append(html);
                }
                page++;


                
            }else{
                if(page==1){
                    $('#con').html('<p style="text-align:center">暂无数据</p>');
                }
                
            }

        });
    }






function setdef(id){
    

    var pa={token:token,id:id};
    api.ajax({
        url: webSiteUrl+'/app/user/setdefaultaddr',
        method: 'post',
        data: {
            values:pa,
        }
    }, function(ret, err) {
        api.hideProgress();
        if(ret.code==200){

        	//alert($('#'+id).prop('id'));
        	$('#'+id).addClass('adreOn').parents(".adreli").siblings().find('.adrespan label').removeClass('adreOn');
            window.location.reload();return;
            api.openWin({
                name: 'loginName',
                url: './myaddr.html',
                reload: true,
                pageParam: {
                    
                }
            });

        }else{
        	//alert($('#'+id).prop('id'));
        }
        
    });  
}
function add() {
    api.openWin({
        name: 'loginName',
        url: './addaddr.html',
        pageParam: {
            
        }
    });
}
function edit(id) {
	api.openWin({
        name: 'member8Name',
        url: './addaddr.html',
        pageParam: {
            id: id,
            type:1,
        }
    });
}
function del(id) {

	var p={token:token,id:id};
    api.ajax({
        url: webSiteUrl+'/app/user/deladdr',
        method: 'post',
        data: {
            values:p,
        }
    }, function(ret, err) {
        api.hideProgress();
        
        if(ret.code==200){
        	//alert(ret.msg);
        	$('#p'+id).remove()
        }
    });
}
</script>

</html>
